//
//  RW_UserSingleViewModel.m
//  SleepApp
//
//  Created by chuangqi on 2021/3/30.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserSingleViewModel.h"
#import "RW_UserSingleItemViewCell.h"

@implementation RW_UserSingleViewModel

#pragma mark -- 功能逻辑
/** 新建用户歌单 */
-(void)addSUserSingleName:(NSString *)singleName
                  success:(void(^)(NSString * response))success
                     fail:(void(^)(NSError *error))fail{
    [[[RW_UserSingleModel createUserSingleName:singleName desc:singleName] then:^id _Nullable(NSString * _Nullable value) {
        if (success){
            success(value);
        }
        return nil;
    }] catch:fail];
}
/** 新建用户歌单关系 */
-(void)addSongToSingleParams:(NSDictionary *)params
                  success:(void(^)(id response))success
                     fail:(void(^)(NSError *error))fail{
    [[[RW_UserSingleModel addSongToSingleParams:params] then:^id _Nullable(NSString * _Nullable value) {
        if (success){
            success(value);
        }
        return nil;
    }] catch:fail];
}

#pragma mark -- 视图模型获取
/** 获取视图模型 */
-(void)getViewSuccess:(void(^)(NSArray<WLBaseCollectionViewLayerSection *> *response))success
                 fail:(void(^)(NSError *error))fail{
    [[[RW_UserSingleModel getUserSingleList] then:^id _Nullable(NSArray<RW_UserSingleModel *> * _Nullable value) {
        if (success){
            success([self getUserSingleSection:value
                                      itemType:UserSingle_Item_Type_UserSingle]);
        }
        return nil;
    }] catch:fail];
}
/** 获取添加收藏视图模型 */
-(void)getAddSingleViewSuccess:(void(^)(NSArray<WLBaseCollectionViewLayerSection *> *response))success
                          fail:(void(^)(NSError *error))fail{
    [[[RW_UserSingleModel getUserSingleList] then:^id _Nullable(NSArray<RW_UserSingleModel *> * _Nullable value) {
        if (success){
            success([self getUserSingleSection:value
                                      itemType:UserSingle_Item_Type_Add_UserSingle]);
        }
        return nil;
    }] catch:fail];
}


#pragma mark -- 视图模型生成
/** 生成收藏歌单列表视图 */
-(NSArray<WLBaseCollectionViewLayerSection *> *)getUserSingleSection:(NSArray<RW_UserSingleModel *> *)singleList
                                                            itemType:(UserSingle_Item_Type)itemType{
    NSMutableArray *array = [NSMutableArray array];
    
    WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
    section.horizontalMaxWidth = Main_Screen_Width;
    
    CGSize itemSize = CGSizeZero;
    //添加收藏的布局
    if (itemType == UserSingle_Item_Type_Add_UserSingle){
        section.insetForSection = UIEdgeInsetsMake(1, 0, 1, 0);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.verticalSection = 1;
        itemSize = [section getCellSizeHeight:45];
    }
    //用户收藏列表布局
    else {
        section.insetForSection = UIEdgeInsetsMake(5, 0, 5, 0);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.verticalSection = 1;
        itemSize = [section getCellSizeHeight:45];
    }
    
    for (RW_UserSingleModel *obj in singleList) {
        RW_UserSingleItemViewCellModel *model = [[RW_UserSingleItemViewCellModel alloc]init];
        model.item_type = itemType;
        model.user_single_model = obj;
        [section.item addObject:[RW_UserSingleItemViewCell getRow:model
                                                         cellSize:itemSize]];
    }
    [array addObject:section];
    return array;
}
@end
